<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

$controller->loadClass('RoleModel', 'role');
$controller->loadClass('KBEntry');
$controller->loadClass('KBEntryModel');


// initialize objects
$rq =& new RequestData($_GET);
$rp =& new RequestData($_POST);
$rp->setHtmlValues('body'); // to skip $_GET['body'] not strip html


$obj = new KBEntry;

$manager =& $obj->setManager(new KBEntryModel());
$manager->checkPriv($priv, $controller->action, @$rq->id, $controller->getMoreParam('popup'));


switch ($controller->action) {
case 'delete': // ------------------------------
	 
	$manager->delete($rq->id);
	$controller->go();

	break;
	

case 'status': // ------------------------------
	
	$manager->status($rq->status, $rq->id);
	$controller->go();

	break;

	
case 'question': // ------------------------------
case 'update': // ------------------------------
case 'insert': // ------------------------------
	
	if(isset($rp->submit)) {
		
		$is_error =& $obj->validate($rp->vars);
		
		if($is_error) {
			$rp->stripVars(true);
			
			$obj->set($rp->vars);
			$obj->setCategory(@$rp->vars['category']);
			
			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
			
			if(!empty($rp->vars['related'])) {
				$ids = implode(',', $rp->vars['related']);
				$obj->setRelated($manager->getRelatedByIds($ids));
			}			
			
			if(!empty($rp->vars['attachment'])) {
				$ids = implode(',', $rp->vars['attachment']);	
				$obj->setAttachment($manager->getAttachmentByIds($ids));
			}
					
		} else {
			$rp->stripVars();
			
			$obj->set($rp->vars);			
			$obj->setCategory($rp->vars['category']);

			if(!empty($rp->vars['category_mirror'])) {
				$obj->setCategoryMirror($rp->vars['category_mirror']);
			}
			
			if(!empty($rp->vars['attachment'])) {
				$obj->setAttachment($rp->vars['attachment']);
			}
			
			if(!empty($rp->vars['related'])) {
				$obj->setRelated($rp->vars['related']);
			}
			
			if(!empty($rp->vars['member_role'])) {
				$obj->setMemberRole($rp->vars['member_role']);
			}
			
			$manager->save($obj);

			if($controller->action == 'question') {
				$manager->setUserEntryPlaced($rq->question_id);
				$controller->setCustomPageToReturn($rq->referer);
			}
			
			$controller->go();
		}
		
	
	} elseif($controller->action == 'update') {
		
		$data = &$manager->getById($rq->id);
		$categories = $manager->getCategoryById($rq->id);
		
		$rp->stripVarsValues($data);
		$obj->set($data);
		$obj->setCategory($categories[1]);
		$obj->setCategoryMirror(@$categories[0]);
		$obj->setRelated($manager->getRelatedById($rq->id));
		$obj->setAttachment($manager->getAttachmentById($rq->id));
		
		if($manager->isExtra('private')) {
			$obj->setMemberRole($manager->getMemberRoleById($rq->id));
		}		
	
	
	} elseif($controller->action == 'question') {
		
		$data = $manager->getMemberQuestionById($rq->question_id);	
		$rp->stripVarsValues($data);
		$obj->set($data);
		$obj->set('id', NULL);
		$obj->set('title', $data['question']);
		$obj->setCategory(array($data['category_id']));
	}	
	
	$view = &$controller->getView($obj, $manager, 'KBEntryView_form'); 

	break;
	

default: // ------------------------------------
	
	$view = &$controller->getView($obj, $manager, 'KBEntryView_list');
}
?>